unit FormAgendamentoUnit;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.ComCtrls, Vcl.ExtCtrls,
  Vcl.StdCtrls, Vcl.Buttons;

type
  TFormAgendamento = class(TForm)
    Panel1: TPanel;
    MonthCalendar1: TMonthCalendar;
    Panel2: TPanel;
    StringGrid1: TStringGrid;
    procedure FormShow(Sender: TObject);
    procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
    procedure MonthCalendar1Click(Sender: TObject);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure AtualizaGrid;
    procedure LimpaGrid;

  end;

var
  FormAgendamento: TFormAgendamento;

implementation

{$R *.dfm}

uses DataModule_Funcionario, FormAdicionarAgendamentoUnit,
  DataModule_Agendamento;

procedure TFormAgendamento.AtualizaGrid;
var
  col,linha,i : integer;
  hora,teste : string;
  dentro : boolean;
  horafim : TTime;
begin
  DM_Agendamento.CDSAgendamento.Close;
  DM_Agendamento.CDSAgendamento.Params[0].AsDate := MonthCalendar1.Date;
  DM_Agendamento.CDSAgendamento.Open;

  DM_Agendamento.CDSAgendamento.First;

  while not DM_Agendamento.CDSAgendamento.eof do
  begin
    hora := copy( DM_Agendamento.CDSAgendamentoHORA.AsString,0,5);
    horafim := DM_Agendamento.CDSAgendamentoDURACAO.AsDateTime + StrToTime(hora);
    for linha := 1 to StringGrid1.RowCount - 1 do
    begin
      teste := StringGrid1.Cells[0,linha];
      if (StringGrid1.Cells[0,linha] = hora) or
      (dentro) then
      begin
        for col := 1 to StringGrid1.ColCount - 1 do
        begin
          if (StringGrid1.Cells[col,0] = DM_Agendamento.CDSAgendamentoFUNCIONARIO.AsString)
          then
          begin
             StringGrid1.Cells[col,linha] := 'Cliente : ' + DM_Agendamento.CDSAgendamentoCLIENTE.AsString+ ' ' +
                                             'Servico : ' + DM_Agendamento.CDSAgendamentoSERVICO.AsString ;
             if StringGrid1.Cells[0,linha] = copy(TimeToStr( horafim),0,5) then
             begin
               dentro := false;
             end
             else
               dentro := true;
          end;
        end;
      end;
    end;
    DM_Agendamento.CDSAgendamento.Next;
  end;
end;

procedure TFormAgendamento.FormShow(Sender: TObject);
var
  i : integer;
begin
  i := 1;
  with DM_Funcionario do
  begin
    CDSFuncionario.Close;
    CDSFuncionario.Open;
    CDSFuncionario.First;
    StringGrid1.ColCount := DM_Funcionario.CDSFuncionario.RecordCount+1 ;
    StringGrid1.RowCount := 26;

    while not CDSFuncionario.Eof do
    begin
      StringGrid1.Cells[i,0] := DM_Funcionario.CDSFuncionarioNOME.AsString;
      i := i+ 1;
      CDSFuncionario.Next;

    end;

    StringGrid1.Cells[0,1] := '08:00';
    StringGrid1.Cells[0,2] := '08:30';
    StringGrid1.Cells[0,3] := '09:00';
    StringGrid1.Cells[0,4] := '09:30';
    StringGrid1.Cells[0,5] := '10:00';
    StringGrid1.Cells[0,6] := '10:30';
    StringGrid1.Cells[0,7] := '11:00';
    StringGrid1.Cells[0,8] := '11:30';
    StringGrid1.Cells[0,9] := '12:00';
    StringGrid1.Cells[0,10] := '12:30';
    StringGrid1.Cells[0,11] := '13:00';
    StringGrid1.Cells[0,12] := '13:30';
    StringGrid1.Cells[0,13] := '14:00';
    StringGrid1.Cells[0,14] := '14:30';
    StringGrid1.Cells[0,15] := '15:00';
    StringGrid1.Cells[0,16] := '15:30';
    StringGrid1.Cells[0,17] := '16:00';
    StringGrid1.Cells[0,18] := '16:30';
    StringGrid1.Cells[0,19] := '17:00';
    StringGrid1.Cells[0,20] := '17:30';
    StringGrid1.Cells[0,21] := '18:00';
    StringGrid1.Cells[0,22] := '18:30';
    StringGrid1.Cells[0,23] := '19:00';
    StringGrid1.Cells[0,24] := '19:30';
    StringGrid1.Cells[0,25] := '20:00';


  end;

  StringGrid1.ColWidths[0] := 64;
  StringGrid1.RowHeights[0] := 20;

  LimpaGrid;
  AtualizaGrid;

end;

procedure TFormAgendamento.LimpaGrid;
var
  linha,col : integer;
begin
  for linha := 1 to StringGrid1.RowCount - 1 do
  begin
    for col := 1 to StringGrid1.ColCount - 1 do
    begin
      StringGrid1.Cells[col,linha] := '';
    end;
  end;
end;

procedure TFormAgendamento.MonthCalendar1Click(Sender: TObject);
begin
  LimpaGrid;
  AtualizaGrid;
end;

procedure TFormAgendamento.StringGrid1DrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
 // DrawText(StringGrid1.Canvas.Handle, PChar(StringGrid1.Cells[aCol, aRow]), Length(StringGrid1.Cells[aCol, aRow]), Rect, DT_WORDBREAK);
end;

procedure TFormAgendamento.StringGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
var
  formAdicionaAgendamento : TFormAdicionarAgendamento;
begin
  formAdicionaAgendamento := TFormAdicionarAgendamento.Create(self);
  with formAdicionaAgendamento do
  begin
    try
      data := DateToStr( MonthCalendar1.Date );
      hora := StringGrid1.Cells[0,ARow];
      funcionario := StringGrid1.Cells[ACol,0];
      ShowModal;
    finally
      Free;
    end;
  end;

end;

end.
